package com.ruoyi.order.controller;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.order.domain.OrdersModel;
import com.ruoyi.order.mapper.OrdersMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.Orders;
import com.ruoyi.order.service.IOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单管理Controller
 * 
 * @author dxp
 * @date 2024-02-13
 */
@RestController
@RequestMapping("/order/order")
public class OrdersController extends BaseController
{
    @Autowired
    private IOrdersService ordersService;
    @Resource
    private OrdersMapper ordersMapper;
    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('order:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrdersModel orders)
    {
        startPage();
        List<OrdersModel> list = ordersService.selectOrdersList(orders);
        return getDataTable(list);
    }
    @GetMapping("/test")
    public Map<String,Object> list()
    {
        Map<String,Object> map = new HashMap<>();
        Map<String, String> data = ordersMapper.getData();
        map.put("totalSales",data.get("totalSales"));
        map.put("totalNumber",data.get("totalNumber"));
        Map<String, String> todayData = ordersMapper.getTodayData();
        map.put("todaySales",todayData.get("totalSales") == null ? 0 : todayData.get("totalSales"));
        map.put("todayNumber",todayData.get("totalNumber") == null ? 0 : todayData.get("totalNumber"));
        List<String> mountData = ordersMapper.getMountData();
        Calendar instance = Calendar.getInstance();
        int i = instance.get(Calendar.MONTH) + 1;
        while (mountData.size() > i){
            mountData.remove(mountData.size() - 1);
        }
        map.put("profitTrendData",mountData);
        return map;
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('order:order:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrdersModel orders)
    {
        List<OrdersModel> list = ordersService.selectOrdersList(orders);
        ExcelUtil<OrdersModel> util = new ExcelUtil<>(OrdersModel.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ordersService.selectOrdersById(id));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('order:order:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Orders orders)
    {
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('order:order:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Orders orders)
    {
        return toAjax(ordersService.updateOrders(orders));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('order:order:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ordersService.deleteOrdersByIds(ids));
    }
}
